﻿using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_QrCodeBusiness : BaseBusiness<PB_QrCode>, IPB_QrCodeBusiness, ITransientDependency
    {
        public PB_QrCodeBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<PB_QrCodeDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var search = input.Search;
            Expression<Func<PB_QrCode, Base_Department, PB_Material, PB_QrCodeDTO>> select = (a, b, c) => new PB_QrCodeDTO
            {
                DepartmentName = b.Name,
                MaterialName = c.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.DeptId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<PB_Material>() on a.MaterialId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b, c);
            if (!search.Keyword.IsNullOrEmpty())
            {
                var keyword = $"%{search.Keyword}%";
                q = q.Where(x =>
                      EF.Functions.Like(x.DepartmentName, keyword) 
                      || EF.Functions.Like(x.MaterialName, keyword)
                      || EF.Functions.Like(x.MaterialCode, keyword)
                      || EF.Functions.Like(x.Code, keyword)
                      || EF.Functions.Like(x.Status, keyword));
            }
            return await q.GetPageResultAsync(input);
        }

        public async Task<PB_QrCode> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_QrCode data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_QrCode data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员


        #endregion
    }
}